<?php
class FckModel extends CommonModel {
	//数据库名称

   public function xiangJiao($Pid=0,$DanShu=1){
        //========================================== 往上统计单数
        $where = array();
        $where['id'] = $Pid;
        $field = 'treeplace,father_id';
        $vo = $this ->where($where)->field($field)->find();
        if ($vo){
            $Fid = $vo['father_id'];//父节点
            $TPe = $vo['treeplace'];//区分左中右
            $table = $this->tablePrefix.'fck';
            if ($TPe == 0 && $Fid > 0){
                $this->execute("update ". $table ." Set `l`=l+$DanShu, `shangqi_l`=shangqi_l+$DanShu  where `id`=".$Fid);
            }elseif($TPe == 1 && $Fid > 0){
                $this->execute("update ". $table ." Set `r`=r+$DanShu, `shangqi_r`=shangqi_r+$DanShu  where `id`=".$Fid);
            }elseif($TPe == 2 && $Fid > 0){
                $this->execute("update ". $table ." Set `lr`=lr+$DanShu, `shangqi_lr`=shangqi_lr+$DanShu  where `id`=".$Fid);
            }
            if ($Fid > 0) $this->xiangJiao($Fid,$DanShu);
        }
        unset($where,$field,$vo);
    }
    
	public function shangjiaTJ($ppath,$treep=0){
		$where = "id in (0".$ppath."0)";
		$lirs = $this->where($where)->order('p_level desc')->field('id,treeplace')->select();
		foreach($lirs as $lrs){
			$myid = $lrs['id'];
			$mytp = $lrs['treeplace'];
			if($treep==0){
				$this->execute("update __TABLE__ Set `re_nums_l`=re_nums_l+1,`re_nums_b`=re_nums_b+1 where `id`=".$myid);
			}else{
				$this->execute("update __TABLE__ Set `re_nums_r`=re_nums_r+1,`re_nums_b`=re_nums_b+1 where `id`=".$myid);
			}
			$treep = $mytp;
		}
		unset($lirs,$lrs,$where);
    }

//	public function xiangJiao($Pid=0,$DanShu=1,$plv=0,$op=1){
//        //========================================== 往上统计单数【有层碰奖】
//
//        $peng = M ('peng');
//        $where = array();
//        $where['id'] = $Pid;
//        $field = 'treeplace,father_id,p_level';
//        $vo = $this ->where($where)->field($field)->find();
//        if ($vo){
//            $Fid = $vo['father_id'];
//            $TPe = $vo['treeplace'];
//            $table = $this->tablePrefix .'fck';
//			$dt	= strtotime(date("Y-m-d"));//现在的时间
//            if ($TPe == 0 && $Fid > 0){
//            	$p_rs = $peng ->where("uid=$Fid and ceng = $op") ->find();
//            	if($p_rs){
//            		$peng->execute("UPDATE __TABLE__ SET `l`=l+{$DanShu}  WHERE uid=$Fid and ceng = $op");
//            	}else{
//            		$peng->execute("INSERT INTO __TABLE__ (uid,ceng,l) VALUES ($Fid	,$op,$DanShu) ");
//            	}
//
//                $this->execute("UPDATE ". $table ." SET `l`=l+{$DanShu}, `benqi_l`=benqi_l+{$DanShu}  WHERE `id`=".$Fid);
//            }elseif($TPe == 1 && $Fid > 0){
//            	$p_rs = $peng ->where("uid=$Fid and ceng = $op") ->find();
//            	if($p_rs){
//            		$peng->execute("UPDATE __TABLE__ SET `r`=r+{$DanShu}  WHERE uid=$Fid and ceng = $op");
//            	}else{
//            		$peng->execute("INSERT INTO __TABLE__ (uid,ceng,r) VALUES ($Fid,$op,$DanShu) ");
//            	}
//                $this->execute("UPDATE ". $table ." SET `r`=r+{$DanShu}, `benqi_r`=benqi_r+{$DanShu}  WHERE `id`=".$Fid);
//            }
//            $op++;-+*
//            if ($Fid > 0) $this->xiangJiao($Fid,$DanShu,$plv,$op);
//        }
//        unset($where,$field,$vo);
//    }

    public function addencAdd($ID=0,$inUserID=0,$money=0,$name=null,$UID=0,$time=0,$acttime=0,$bz=""){
        //添加 到数据表
        if ($UID > 0) {
            $where = array();
            $where['id'] = $UID;
            $frs = $this->where($where)->field('nickname')->find();
            $name_two = $name;
            $name = $frs['nickname'] . ' 开通会员 ' . $inUserID ;
            $inUserID = $frs['nickname'];
        }else{
            $name_two = $name;
        }

        $data = array();
        $history = M ('history');

        $data['user_id']		= $inUserID;
        $data['uid']			= $ID;
        $data['action_type']	= $name;
        if($time >0){
        	$data['pdt']		= $time;
        }else{
        	$data['pdt']		= mktime();
        }
        $data['epoints']		= $money;
        if(!empty($bz)){
        	$data['bz']			= $bz;
        }else{
        	$data['bz']			= $name;
        }
        $data['did']			= 0;
        $data['type']			= 1;
        $data['allp']			= 0;
        if($acttime>0){
        	$data['act_pdt']	= $acttime;
        }
        $result = $history ->add($data);
        unset($data,$history);
    }

    public function huikuiAdd($ID=0,$tz=0,$zk,$money=0,$nowdate=null){
        //添加 到数据表

        $data                   = array();
        $huikui                = M ('huikui');
        $data['uid']            = $ID;
        $data['touzi']    = $tz;
        $data['zhuangkuang']            = $zk;
        $data['hk']        = $money;
        $data['time_hk']             = $nowdate;
        $huikui ->add($data);
        unset($data,$huikui);
    }


    //对碰1：1
    public function touch1to1(&$Encash,$xL=0,$xR=0,&$NumS=0)
    {
        $xL = floor($xL);
        $xR = floor($xR);

        if ($xL > 0 && $xR > 0) {

            if ($xL > $xR) {
                $NumS = $xR;
                $xL = $xL - $NumS;
                $xR = $xR - $NumS;
                $Encash['0'] = $Encash['0'] + $NumS;//$Encash['0']左碰掉的数
                $Encash['1'] = $Encash['1'] + $NumS;//$Encash['1']右碰掉的数

            }
            if ($xL < $xR) {
                $NumS = $xL;
                $xL = $xL - $NumS;
                $xR = $xR - $NumS;
                $Encash['0'] = $Encash['0'] + $NumS;
                $Encash['1'] = $Encash['1'] + $NumS;
            }
            if ($xL == $xR) {
                $NumS = $xL;
                $xL = 0;
                $xR = 0;
                $Encash['0'] = $Encash['0'] + $NumS;
                $Encash['1'] = $Encash['1'] + $NumS;
            }
            $Encash['2'] = $NumS;
            //$this->touch1to1($Encash,$xL, $xR,$NumS=0);
        } else {
            $NumS = 0;
            $Encash['0'] = 0;
            $Encash['1'] = 0;
        }

    }
 
//============对碰奖================== 
  
 public function duipeng($ppath,$user_level,$u_p_level){
    	$fee = M ('fee');
    	$fee_rs = $fee->field('s3,str1,s9,s10,str5')->find(1);
    	$s3 = explode("|",$fee_rs['s3']);		//各级对碰比例
    	//$s10 = explode("|",$fee_rs['s10']);		//各级对碰比例
    	$s9 = explode("|",$fee_rs['s9']);		//会员级别费用（投资金额）
    	$s5 = explode("|",$fee_rs['str1']);		//封顶
    	//$one_mm = $s9[0];
    	$fck_array = 'is_pay>=1 and (shangqi_l>0 or shangqi_r>0) and id in (0'.$ppath.'0)';
    	$field = 'id,user_id,shangqi_l,shangqi_r,benqi_l,benqi_r,is_fenh,p_path,re_nums,nickname,u_level,re_id,day_feng,re_path,re_level,p_level,peng_num,n_pai';
    	$frs = $this->where($fck_array)->field($field)->select();
    	//BenQiL  BenQiR  ShangQiL  ShangQiR
    	foreach ($frs as $vo){
    		$L = 0;
    		$R = 0;
    		$L = $vo['shangqi_l'];//上期左区剩余
    		$R = $vo['shangqi_r'];//上期右区区剩余
    		$sq_l = $vo['shangqi_l'];
    		$sq_r = $vo['shangqi_r'];
    		$Encash    = array();
    		$NumS      = 0;//碰数
    		$money     = 0;//对碰奖金额
    		$Ls        = 0;//左剩余
    		$Rs        = 0;//右剩余
    		$this->touch1to1($Encash, $L, $R, $NumS);
    		$Ls = $L - $Encash['0'];//左余数
    		$Rs = $R - $Encash['1'];//右左余数
    		$myid = $vo['id'];
    		$myusid = $vo['user_id'];
            $myulv = $vo['u_level'];//会员等级数
    		$ss = $myulv-1;
    		$feng = $vo['day_feng'];
    		$is_fenh = $vo['is_fenh'];
    		$reid = $vo['re_id'];
    		$repath = $vo['re_path'];
    		$ul = $s3[$ss]/100;
    		$money = $s9[$ss]*$NumS *$ul;//对碰奖 奖金
    		if($s5[$ss]<$money){
    			$money = $s5[$ss];
    		}
    		if($feng>=$s5[$ss]){
    			$money=0;
    		}else{
    			$jfeng=$feng+$money;
    			if ($jfeng>$s5[$ss]){
    				$money=$s5[$ss]-$feng;
    			}
    		}
    		$this->query('UPDATE __TABLE__ SET `shangqi_l`='. $Ls .',`shangqi_r`='. $Rs .' where `id`='. $vo['id'].' and shangqi_l='.$sq_l.' and shangqi_r='.$sq_r);
    		$money_count = $money;
    		if($money_count>0&&$is_fenh==0){
    			$this->rw_bonus($myid,$myusid,2,$money_count);
    			//领导奖
    			$this->lingdaojiang($repath,$myusid,$money_count);
    		}
    	}
    	unset($fee,$fee_rs,$frs,$vo);
    }
//==================================================
    public function duipengdailo($ppath,$myid,$user_level,$u_p_level,$money=0){
        switch($user_level){
            case$user_level==0:
                $max=3;break;
            case$user_level==1:
                $max=5;break;
            case$user_level==2:
                $max=7;break;
        }
        $lirsv = $this->where('father_id in (0'.$ppath.'0) and is_fenh=0')->field('id')->order('p_level desc')->limit($max)->select();
        foreach( $lirsv as $rs){
            $fid = $rs['id'];
            $num=$this->where('father_id='.$fid. 'and is_pay>0 and is_fenh=0 and p_level='.$u_p_level)->count();
            if($num=0){
                $money=0;
            }elseif($num>0){
                $money2=$money*5/100/$num;
            }

        }


    }
//====================================================
    //层奖b1
    public function shuangcengpeng($p_path,$inUserID=0,$p_level=0){
    $fee = M('fee');
    $fee_rs = $fee->field('s12,s9')->find(1);
    $s12 =$fee_rs['s12']/100;
    $s9=explode("|",$fee_rs['s9']);
    $lirs = $this->where('id in (0'.$p_path.'0) and is_fenh=0')->field('id,u_level,get_ceng,p_level')->order('p_level desc')->select();
    
    foreach ($lirs as $lrs){
        $myid = $lrs['id'];//查出自己的所有的父id
        $ceng = $lrs['get_ceng'];
        $fa_u_level = (int)$lrs['u_level'];//父亲的等级
        $plevel = $lrs['p_level'];//父类的绝对层数
        $relevel=$p_level-$plevel;//相对层数
		
        $lnn = 0;
        $rnn = 0;
        $l_rs = $this->where('father_id='.$myid)->field('id')->select();//自己父id的id
        $i=0;
        foreach($l_rs as $voo){
            $l_id = $voo['id'];
            $lnn = $this->where('(p_path like "%,'.$l_id.',%" or id='.$l_id.') and is_pay>0 and p_level='.$p_level)->count();
            if($lnn > 0){
                $urs= $this->where('is_pay>0 and p_level='.$p_level)->field('u_level')->order('u_level desc')->limit(1)->find();
               $maxnum[$i]=(int)$urs['u_level'];//查找同层的最大的会员
                $i++;
            }
           // $l_id."<br/>";
        }
        $maxnum=min($fa_u_level,$maxnum[0],$maxnum[1]);//对比结果


       unset($l_rs, $urs);

        if(($i >=2)&&($relevel>$ceng)){
            $money_count =$s9[$maxnum-1]*$s12;
			
        //	$money_count = $this->zfd_jj($myid,$money_count); //封顶
            if($money_count>0){
                $this->rw_bonus($myid,$inUserID,8,$money_count);
                $this->execute("update __TABLE__ set get_ceng=". $relevel." where get_ceng< $relevel and id=".$myid);
            }
            //break;
        }
    }
    unset($lirs,$lrs,$maxnum);
    }

    //=============对碰奖的日封顶=========================
	
    public function zfd_jj($uid,$money=0){
        $fee = M('fee');
        $fee_rs = $fee->field('str1')->find();
        $str1 = explode("|",$fee_rs['str1']);//分红奖封顶


        $rs = $this->where('id='.$uid)->field('u_level,day_feng')->find();
        if($rs){
            $day_feng = $rs['day_feng'];
            $feng = $str1[$rs['u_level']-1];
            if($money > $feng){
                $money = $feng;
            }

            if($day_feng >= $feng){
                $money = 0;
            }else{
                $tt_money = $money + $day_feng;
                if( $tt_money > $feng){
                    $money = $feng-$day_feng;
                }
            }
        }

        return $money;
    }


    //=====见点奖    拿双区20层见点5元。（伞下20层，每个位置见人就可以获得5元）
    public function jiandianjiang($ppath,$inUserID=0,$user_u_level){

        $fee = M('fee');
        $fee_rs = $fee->field('str9,s9')->find(1);
        $str9 = explode("|",$fee_rs['str9']);//见点奖设置
        $s9=explode("|",$fee_rs['s9']);
        $max_c = $str9[0];

        $lirs = $this->where('id in (0'.$ppath.'0)')->field('id,u_level,is_fenh')->order('p_level desc')->limit($max_c)->select();
        $i = 1;
        foreach($lirs as $lrs){
            $myid = $lrs['id'];
            $is_fenh = $lrs['is_fenh'];
            $fa_u_level=(int)$lrs['u_level'];

            $minnum=min($fa_u_level,(int)$user_u_level);

            $money_count = $s9[$minnum-1]*$str9[1]/100;//见点奖金
			
            if($money_count>0&&$is_fenh==0){
                $this->rw_bonus($myid,$inUserID,5,$money_count);
            }
            $i++;
        }
        unset($fee,$fee_rs,$lirs,$lrs);
    }

//===================计算奖金=====================1
    public function getusjj($uid,$type=0){
    	$mrs = $this->where('id='.$uid)->find();
    	if($mrs){
		
			$this->shuangcengpeng($mrs['p_path'],$mrs['user_id'],$mrs['p_level']);
			
			$this->duipeng($mrs['p_path']);//自己的路径
			
			$this->tuijj($mrs['re_id'],$mrs['user_id'],$mrs['cpzj']);
		
			$this->guanglij($mrs['re_path'],$mrs['user_id'],$mrs['u_level']);
		
            $this->lingdaojiang($mrs['re_path'],$mrs['user_id'],$mrs['u_level']);
        
			$this->jiandianjiang($mrs['p_path'],$mrs['user_id'],$mrs['u_level']);
	     	if($type==1){
    			//报单奖
    			$this->baodanfei($mrs['shop_id'],$mrs['user_id'],$mrs['cpzj']);
    		}
    	}
    	$this->getLevel();
		unset($mrs);
    }

    //推荐奖 推薦獎 10%
    public function tuijj($ID=0,$inUserID=0,$cpzj=0){//父id，自己的id，投资金额
        //$inUserID自己的会员编号id
    	$fee = M('fee');
    	$fee_rs = $fee->field('s4')->find(1);
    	$s4 = $fee_rs['s4']/100;

    	$where = array();
    	$where['id'] = $ID;
    	$where['is_fenh'] = array('eq',0);
    	$field = 'id,u_level,re_path';
    	$frs = $this->where($where)->field($field)->find();
    	if ($frs){
    		$myid = $frs['id'];//推荐id
    		$ssss = $frs['u_level']-1;
    		$prii = $s4;
    		$money_count = bcmul($cpzj, $prii,2);
    		if($money_count>0){
    			$this->rw_bonus($myid,$inUserID,1,$money_count);
				
			    $this->guanglij($frs['re_path'],$inUserID,$money_count);
          }
    	}
    	unset($fee,$fee_rs,$frs,$where);
    }  
  


   public function getReid($id){
   		$rs = $this->where('id='.$id)->field('id,re_nums,is_fenh')->find();
   		return array('re_id'=>$rs['id'],'re_nums'=>$rs['re_nums'],'is_fenh'=>$rs['is_fenh']); 
   }
    
	//管理奖 推荐1单拿1代推荐奖的10%，推荐2单拿2代推荐奖的10%，以此类推，推荐10单拿10代推荐奖的10%。
    public function guanglij($repath,$inUserID=0,$money=0){
  
    	$fee = M('fee');
    	$fee_rs = $fee->field('s7,s15')->find(1);
    	$s7 = $fee_rs['s7']/100;  //管理奖的奖金金额（%）
    	$s15 = explode("|",$fee_rs['s15']); //代数
    
    	$scc = count($s15);
    	$max_c = 0;
    	for($i=0;$i<$scc;$i++){
    		if($s15[$i]>$max_c){
    			$max_c = $s15[$i];
    		}
    	}
    	
    	$lirs = $this->where('id in (0'.$repath.'0)')->field('id,re_nums,is_fenh,re_id')->order('re_level desc')->limit($max_c)->select();
    	//推荐的人数
       
    	$i = 1;
		foreach($lirs as $lrs){
			$myid = $lrs['id'];
			//$array = $this->getReid($lrs['re_id']);
		
			$is_fenh = $lrs['is_fenh'];
			$re_nums = $lrs['re_nums'];
			//$re_id = $array['re_id'];
			
			if($re_nums >10){
				$re_nums = 10;
			}
			
			$sss = $re_nums-1;
			$myccc = $s15[$sss];
			
			$money_count =10;
			if($myccc>=$i){
				$money_count = bcmul($money,$s7,2);
			}
			
			//dump($money_count);
			
			if($money_count>0&&$is_fenh==0){
				$this->rw_bonus($myid,$inUserID,4,$money_count);
			}
			$i++;
		}
        unset($fee,$fee_rs,$s15,$lirs,$lrs);
    }



    //领导奖 直推1單享受1代，直推2單享受2代，以此類推，最高可享受10代每代对碰的10%。
    public function lingdaojiang($repath,$inUserID=0,$money=0){  //推荐路径 自己的id 相应的奖金

        $fee = M('fee');
        $fee_rs = $fee->field('s1,s11')->find(1);
		$s1 = $fee_rs['s1']/100;  //管理奖的奖金金额（%）
        $s11 = explode("|",$fee_rs['s11']); //代数
        $scc = count($s11);
		$max_c = 0;
        for($i=0;$i<$scc;$i++){
            if($s11[$i]>$max_c){
                $max_c = $s11[$i];
            }
        }

        $lirs = $this->where('id in (0'.$repath.'0)')->field('id,re_nums,is_fenh,re_id')->order('re_level desc')->limit($max_c)->select();
		
        //推荐的人数

        $i = 1;
        foreach($lirs as $lrs){
            $myid = $lrs['id'];
            $array = $this->getReid($lrs['re_id']);
            $is_fenh = $lrs['is_fenh'];
            $re_nums = $lrs['re_nums'];
            $re_id = $array['re_id'];

            if($re_nums >10){
			   $re_nums = 10;
            }

            $sss = $re_nums-1;
			$myccc = $s11[$sss];

            $money_count = 10;
            if($myccc>=$i){
                $money_count = bcmul($money,$s1,2);//$money*$s1结果保留2位小数
            }

            //dump($money_count);

            if($money_count>0&&$is_fenh==0){
                $this->rw_bonus($myid,$inUserID,3,$money_count);
            }
            $i++;
        }
        unset($fee,$fee_rs,$s11,$lirs,$lrs);
    }
    
    //升级
    public function getLevel(){
    	$mm = 1000;
    	
    	//一星
    	$this->execute("update __TABLE__ set get_level=1 where get_level=0 and xy_money>=".$mm);//xy_money存未扣税的所有收入   
    	
    	//二星
    	$list = $this->where('is_pay>0 and re_nums>=2 and get_level=1')->field('id')->order('re_level asc')->select();
   
    	foreach($list as $voo){
    		$rs = $this->where('re_id='.$voo['id'])->field('id')->select();
    		
    		$xx1 = 0;
    
    		foreach ($rs as $vo){	
    			//二星
    			$where = "((re_path like '%,".$vo['id'].",%') or id=".$vo['id'].") and get_level=1";
    			$rs_count = $this->where($where)->count();
    	
    			if($rs_count){
    				$xx1 +=1;
    			}
    			if($xx1 >= 2){
    				break;
    			}
    		}
    	
    		if($xx1 >= 2){
    			$this->execute("update __TABLE__ set get_level=2 where get_level=1 and id =".$voo['id']);
    			$xx1 = 0;
    		}		
    	}
    	unset($list,$voo,$rs,$vo,$where,$rs_count,$xx1);
   
    	//三星
    	$list = $this->where('is_pay>0 and re_nums>=2 and get_level=2')->field('id')->order('re_level asc')->select();
    	foreach($list as $voo){
    		$rs = $this->where('re_id='.$voo['id'])->field('id')->select();
    	
    		$xx2 = 0;
    		foreach ($rs as $vo){
    			//二星
    			$where = "((re_path like '%".$vo['id']."%') or id=".$vo['id'].") and get_level=2";
    			$rs_count = $this->where($where)->count();
    			if($rs_count){
    				$xx2 +=1;
    				if($xx2 >= 2){
    					break;
    				}
    			}
    		}
    		if($xx2 >= 2){
    			$this->execute("update __TABLE__ set get_level=3 where get_level=2 and id=".$voo['id']);
    		}
    	}
    	unset($list,$voo,$rs,$vo,$where,$rs_count,$xx2);
    	
    	//四星
    	$list = $this->where('is_pay>0 and re_nums>=2 and get_level=3')->field('id')->order('re_level asc')->select();
    	foreach($list as $voo){
    		$rs = $this->where('re_id='.$voo['id'])->field('id')->select();
    		 
    		$xx3 = 0;
    		foreach ($rs as $vo){
    			$where = "((re_path like '%".$vo['id']."%') or id=".$vo['id'].") and get_level=3";
    			$rs_count = $this->where($where)->count();
    			if($rs_count){
    				$xx3 +=1;
    				if($xx3 >= 3){
    					break;
    				}
    			}
    		}
    		if($xx3 >= 3){
    			$this->execute("update __TABLE__ set get_level=4 where get_level=3 and id=".$voo['id']);
    		}
    	}
    	unset($list,$voo,$rs,$vo,$where,$rs_count,$xx3);
	
    }
    

    
    //领导奖b3
   // public function lingdaojiang($id,$mm=0){
//    	$fee = M('fee');
//    	$fee_rs = $fee->field('s1,s11')->find(1);
//    	$s1 = explode("|",$fee_rs['s1']); //百分比
//
//		$rs = $this->where('id='.$id)->field('id,user_id,get_level')->find();
//		$is_fenh = $rs['is_fenh'];//是否设置有奖项
//		
//    	$prii =$s1[$rs['get_level']-1]/100; 
//
//    	$money_count = bcmul($mm,$prii,2);//将二个高精确度数字相乘，后面的是返回值小数的位数。
//    	
//    	if($money_count>0&&$is_fenh==0){
//    		$this->rw_bonus($id,$rs['user_id'],3,$money_count);//扣税
//    	}
//    
//    	unset($where,$fee,$fee_rs);
//    }
//	

    
	
    //报单费
    public function baodanfei($uid,$inUserID,$cpzj=0){
    	$bonus = M ('bonus');
		$fee = M('fee');
    	$fee_rs = $fee->field('s14')->find();
		$s14 = $fee_rs['s14'];
		$money_count =$cpzj*$s14/100;
		$frs = $this->where('id='.$uid.' and is_pay>0 and is_fenh=0')->field('id,user_id,u_level')->find();
        //var_dump($frs);
		if($frs){
			$myid = $frs['id'];
			if($money_count>0){
               // echo $money_count;
                $this->rw_bonus($myid,$inUserID,6,$money_count);
			}
		}
	    unset($bonus,$fee,$fee_rs,$frs,$s14);
    }


	


    	

	//各种扣税   写入数据库
    public function rw_bonus($myid,$inUserID=0,$bnum=0,$money_count=0,$u_level=0){
    	$fee = M('fee');
    	$fee_rs = $fee->field('s9,str4,str7,str5,s5,s6')->find();//投资金额，购物币，网络管理费，励基金，重销奖的百分比
    	$s9 = explode('|',$fee_rs['s9']);
    
    	$s6 = $fee_rs['s6']/100; //重消奖
    	$str5 = $fee_rs['str5'];  //网络管理费
    	
    	$str4 = $fee_rs['str4'];  //进入购物币部分
    	$s5 = $fee_rs['s5'];  //进入励基金

        $str7=$money_count*$fee_rs['str7']/100;//个人所得税

        $money_ka = 0;
    	$money_kb = 0;
    	$money_kc = 0;
    	$money_kd = 0;
    	
    	$money_ka = $money_count*$s6;//重消费
    	$money_kc = ((int)($money_ka*$str4))/100;// 进入购物币部分
    	$money_kd = ((int)($money_ka*$s5))/100;//进入励基金
    	
    	$last_money_ka = $money_ka - $money_kc - $money_kd;//重消费剩余
    	$last_n = $money_count-$money_ka-$str7;//奖金第一次剩余
    	
    	$usqla = "";
    	//网络费
    	$mrs = $this->where('id='.$myid.' and wlf=0')->field('id,user_id,wlf,wlf_money')->find();
    	if($mrs){
    		$uuid = $mrs['id'];
    		$inUserID_did = $mrs['user_id'];//自己的id
    		$wlf = $mrs['wlf'];//网络费时间
    		$wlf_money = $mrs['wlf_money'];//网络费
    		$all_mm = $wlf_money+$last_n;//如果网络费没有交齐
    		$k_mon = 0;
    		if($all_mm>=$str5){
    			$k_mon = $str5-$wlf_money;//还差多少网络费
    			$this->execute("UPDATE __TABLE__ SET wlf=".time()." where `id`=".$uuid." and wlf=0");
    		}else{
    			$k_mon = $last_n;
    		}
    		if($k_mon<0){
    			$k_mon = 0;
    		}
    		if($k_mon>0){
    			$money_kb = $k_mon;
    			$usqla .= ",wlf_money=wlf_money+".$money_kb;
    		}
    	}
    	unset($mrs);
    	   
		
		$last_m = $money_count-$money_ka-$money_kb-$str7;//剩余，此值写入现金账户
	
    	$bonus = M('bonus');
    	$bid = $this->_getTimeTableList($myid);
    	$inbb = "b".$bnum;
    	
//     	if($bnum==1 || $bnum==2 ){
//     		$usqla = ",day_feng=day_feng+".$money_count.""; 
//     	}
    	
    	$usqlc = "agent_use=agent_use+".$last_m.",agent_xf=agent_xf+".$money_kc.",agent_cf=agent_cf+".$last_money_ka.",re_money=re_money+".$money_ka.",agent_gp=agent_gp+".$money_kd; //agent_cf重消奖
    	
    	$bonus->execute("UPDATE __TABLE__ SET b0=b0+".$last_m.",".$inbb."=".$inbb."+".$money_count.",b7=b7-".$str7."  WHERE id={$bid}"); //加到记录表
    	$this->execute("update __TABLE__ set ".$usqlc.",zjj=zjj+".$last_m.",xy_money=xy_money+".$money_count.$usqla." where id=".$myid);//加到fck
		
    	unset($bonus);

    	if($money_count>0){//奖金值
    		$this->addencAdd($myid,$inUserID,$money_count,$bnum);
    	}
        if($money_ka>0){//扣税
            $this->addencAdd($myid,$inUserID,-$str7,7);
        }
    	if($money_ka>0){//重消费
    		$this->addencAdd($myid,$inUserID,-$money_ka,11);
    	}
    	if($money_kb>0){//扣除的网络费
    		$this->addencAdd($myid,$inUserID_did,-$money_kb,9);
    	}
    	
    	if($money_kc>0){//购物币
    		$this->addencAdd($myid,$inUserID,-$money_kc,7,0,0,0,'进入购物币部分');
    	}
    	if($money_kd>0){//励基金
    		$this->addencAdd($myid,$inUserID,-$money_kd,8,0,0,0,'进入励基金部分');
    	}
        if($bnum!=3){
            $this->lingdaojiang($myid,$money_count);
        }




        	unset($fee,$fee_rs,$s9,$mrs);
    }
    
    //重复消费月封顶
    public function month_fd($uid,$money=0){
    	$fee = M('fee');
    	$fee_rs = $fee->field('s5')->find();
    	$s5 =$fee_rs['s5'];//封顶
    	 
    	$rs = $this->where('id='.$uid)->field('u_level,re_money')->find();
    	if($rs){
    		$month_feng = $rs['re_money'];
    		
    		if($money > $s5){
    			$money = $s5;
    		}
    		 
    		if($month_feng >= $s5){
    			$money = 0;
    		}else{
    			$tt_money = $money + $month_feng;
    			if( $tt_money > $s5){
    				$money = $s5-$month_feng;
    			}
    		}
    	}
    	 
    	return $money;
    }
    
    
    
    
    //分红添加记录
    public function add_xf($one_prices=0,$cj_ss=0){
		$fenhong = M('fenhong');
		$data = array();
// 		$data['uid'] = 1;
// 		$data['user_id'] = $cj_ss;
		$data['f_num'] = $cj_ss;
		$data['f_money'] = $one_prices;
		$data['pdt'] = mktime();
		$fenhong->add($data);
		unset($fenhong,$data);
    }

	//日封顶
    public function ap_rifengding(){

    	$fee = M('fee');
    	$fee_rs = $fee->field('s7')->find();
    	$s7 = explode("|",$fee_rs['s7']);

    	$where=array();
    	$where['b8'] = array('gt',0);
    	$mrs=$this->where($where)->field('id,b8,day_feng,get_level')->select();
    	foreach($mrs as $vo){
    		$day_feng = $vo['day_feng'];
    		$ss = $vo['get_level'];
    		$bbb = $vo['b8'];
    		$fedd = $s7[$ss];//封顶
			$get_money = $bbb;
    		$all_money = $bbb+$day_feng;
    		$fdok = 0;
    		if($all_money>=$fedd){
    			$fdok = 1;
    			$get_money = $fedd-$day_feng;
    		}
    		if($get_money<0){
    			$get_money = 0;
    		}
    		if($get_money>=0){
    			$this->query("UPDATE __TABLE__ SET `b8`=".$get_money.",day_feng=day_feng+".$get_money." where `id`=".$vo['id']);
    		}
    		if($get_money>0){
    			if($fdok==1){
    				$this->query("UPDATE __TABLE__ SET x_num=x_num+1 where `id`=".$vo['id']);
    			}
    		}
    	}
    	unset($fee,$fee_rs,$s7,$where,$mrs);
    }

	//总封顶
    public function ap_zongfengding(){

    	$fee = M('fee');
    	$fee_rs = $fee->field('s15')->find();
    	$s15 = $fee_rs['s15'];

    	$where=array();
    	$where['b0'] = array('gt',0);
    	$where['_string'] = 'b0+zjj>'.$s15;
    	$mrs=$this->where($where)->field('id,b0,zjj')->select();
    	foreach($mrs as $vo){
    		$zjj = $vo['zjj'];
    		$bbb = $vo['b0'];
    		$get_money = $s15-$zjj;

    		if($get_money>0){
    			$this->query("UPDATE __TABLE__ SET `b0`=".$get_money." where `id`=".$vo['id']);
    		}
    	}
    	unset($mrs);
    }

	//奖金大汇总（包括扣税等）
    public function quanhuizong(){

    	$this->execute('UPDATE __TABLE__ SET `b0`=b1+b2+b3+b4+b5+b6+b7+b8');

    	$this->execute('UPDATE __TABLE__ SET `b0`=0,b1=0,b2=0,b3=0,b4=0,b5=0,b6=0,b7=0,b8=0,b9=0,b10=0 where is_fenh=1');

    }


    //清空日封顶
	public function emptyTime(){

		$nowdate = strtotime(date('Y-m-d'));//天

		$this->query("UPDATE `xt_fck` SET `day_feng`=0,_times=".$nowdate." WHERE _times !=".$nowdate."");

	}
	
	
	//清空月封顶
	public function emptyMonthTime(){  //zyq_date 记录当前月
	
		$nowmonth = date('m');
	
		$this->query("UPDATE `xt_fck` SET `re_money`=0,zyq_date=".$nowmonth." WHERE zyq_date !=".$nowmonth."");
	
	}

	public function gongpaixtsmall($uid){
		$fck = M ('fck');
		$mouid=$uid;
		$field = 'id,user_id,p_level,p_path,u_pai';
		$where = 'is_pay>0 and (p_path like "%,'.$mouid.',%" or id='.$mouid.')';
	
		$re_rs = $fck ->where($where)->order('p_level asc,u_pai asc')->field($field)->select();
		$fck_where = array();
		foreach($re_rs as $vo){
			$faid=$vo['id'];
			$fck_where['is_pay']   = array('egt',0);
			$fck_where['father_id']   = $faid;
			$count = $fck->where($fck_where)->count();
			if ( is_numeric($count) == false){
				$count = 0;
			}
			if ($count<2){
				$father_id=$vo['id'];
				$father_name=$vo['user_id'];
				$TreePlace=$count;
				$p_level=$vo['p_level']+1;
				$p_path=$vo['p_path'].$vo['id'].',';
				$u_pai=$vo['u_pai']*2+$TreePlace;
	
				$arry=array();
				$arry['father_id']=$father_id;
				$arry['father_name']=$father_name;
				$arry['treeplace']=$TreePlace;
				$arry['p_level']=$p_level;
				$arry['p_path']=$p_path;
				$arry['u_pai']=$u_pai;
				return $arry;
				break;
			}
		}
	}
    public function bobifengding(){

		$fee = M ('fee');
		$bonus = M ('bonus');
		$fee_rs = M ('fee') -> find();
    	$table = $this->tablePrefix .'fck';
    	$z_money = 0;//总支出
        $z_money = $this->where('is_pay = 1')->sum('b2');
        $times = M ('times');
        $trs = $times->order('id desc')->field('shangqi')->find();
        if ($trs){
            $benqi = $trs['shangqi'];
        }else{
            $benqi = strtotime(date('Y-m-d'));
        }
        $zsr_money = 0;//总收入
        $zsr_money = $this->where('pdt>='. $benqi .' and is_pay=1')->sum('cpzj');
        $bl = $z_money / $zsr_money ;
        $fbl = $fee_rs['s11'] / 100;
        if ($bl > $fbl){
            //$bl = $fbl;
            //$xbl = $bl - $fbl;
            $z_o1=$zsr_money*$fbl;
            $z_o2=$z_o1/$z_money;
            $this->query("UPDATE ". $table ." SET `b2`=b2*{$z_o2} where `is_pay`>=1 ");
        }



    }


	public  function _getTimeTableList($uid)
    {
    	$times = M ('times');
    	$bonus = M ('bonus');
    	$boid = 0;
    	$nowdate = strtotime(date('Y-m-d'))+3600*24-1;
    	$settime_two['benqi'] = $nowdate;
    	$settime_two['type']  = 0;
    	$trs = $times->where($settime_two)->find();
    	if (!$trs){
    		$rs3 = $times->where('type=0')->order('id desc')->find();
    		if ($rs3){
    			$data['shangqi']  = $rs3['benqi'];
    			$data['benqi']    = $nowdate;
    			$data['is_count'] = 0;
    			$data['type']     = 0;
    		}else{
    			$data['shangqi']  = strtotime('2010-01-01');
    			$data['benqi']    = $nowdate;
    			$data['is_count'] = 0;
    			$data['type']     = 0;
    		}
    		$shangqi = $data['shangqi'];
    		$benqi   = $data['benqi'];
    		unset($rs3);
    		$boid = $times->add($data);
    		unset($data);
    	}else{
    		$shangqi = $trs['shangqi'];
    		$benqi   = $trs['benqi'];
    		$boid = $trs['id'];
    	}
    	$_SESSION['BONUSDID'] = $boid;
    	$brs = $bonus->where("uid={$uid} AND did={$boid}")->find();
    	if ($brs){
    		$bid = $brs['id'];
    	}else{
    		$frs = $this->where("id={$uid}")->field('id,user_id')->find();
    		$data = array();
    		$data['did'] = $boid;
    		$data['uid'] = $frs['id'];
    		$data['user_id'] = $frs['user_id'];
    		$data['e_date'] = $benqi;
    		$data['s_date'] = $shangqi;
    		$bid = $bonus->add($data);
    	}
    	return $bid;
    }

}
?>